package d0516_util包;

import java.util.*;
/**
 * 数组工具类
 */
public class Arrays和Random类 {
	
	public static void main(String[] args) {
		
		// seed 随机种子  伪随机
		System.out.println("============随机对象===========");
		int seed = 1;
		Random random1 = new Random(seed);
		Random random2 = new Random(seed);
		System.out.println(random1.nextInt());
		System.out.println(random1.nextInt());
		System.out.println(random1.nextInt());
		System.out.println(random1.nextInt());
		System.out.println(random1.nextInt());
		System.out.println("-----------------");
		System.out.println(random2.nextInt());
		System.out.println(random2.nextInt());
		System.out.println(random2.nextInt());
		System.out.println(random2.nextInt());
		System.out.println(random2.nextInt());
	
		
		
		System.out.println("============数组工具类===========");
		System.out.println("---------填充--------");
		int[] arr = new int[100];
		Arrays.fill(arr, 100);
		// 增强for循环
		for(int a : arr) {
			System.out.println(a);
		}
		
		System.out.println("---------排序--------");
		for (int i = 0; i < arr.length; i++) {
			arr[i] = random1.nextInt(50);
		}
		Arrays.sort(arr);
		for(int i=0; i<arr.length; i++) {
			System.out.println(arr[i] + "==>" + i);
		}
		
		System.out.println("---------排序:学生年龄排序--------");
		Student[] s = new Student[100];
		for (int i = 0; i < s.length; i++) {
			s[i] = new Student( "张三" + i, random1.nextInt(100));
		}
		/**
		 * 匿名内部类   Comparator 接口 ==>   SWT监听器
		 */
		Comparator<Student> c = new Comparator<Student>() {
		@Override
		public int compare(Student o1, Student o2) {
			return o2.age - o1.age;
		}};
		
		// sort 排序方法要求, 每个数组元素对象必须都是实现排序接口 Comparable
		Arrays.sort(s, c);
		for(Student stu : s) {
			System.out.println(stu);
		}
		
		System.out.println("============查找: 二叉查找===========");
		/**
		 * arr 数组必须排序后才能查找
		 */
		int index = Arrays.binarySearch(arr, 20);
		System.out.println(index);
	}
	
	/**
	 * 扩展练习题:
	 * 请给学生添加三科成绩: 语文,数学,历史
	 * 随机赋值:分数  60~100
	 * 根据总分排序(降序)
	 */

}


/**
 * 
 * Comparable 泛型接口
 *
 */
class Student/* implements Comparable<Student>*/{
	String name;
	int age;
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	/**
	 * 用当前对象和传入的对象进行比较, 返回值数字
	 */
	/*@Override
	public int compareTo(Student o) {
		return o.age - this.age;
	}*/

}






